package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheStore;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodOrderDO;
import top.lixunda.ecommerce.server.good.api.entity.db.OrderApplyDO;
import top.lixunda.ecommerce.server.good.api.entity.vo.OrderApplyWithOrderVO;
import top.lixunda.ecommerce.server.good.mapper.GoodOrderMapper;
import top.lixunda.ecommerce.server.good.mapper.OrderApplyMapper;
import top.lixunda.ecommerce.server.good.service.IOrderApplyService;

/**
 * 订单申请服务接口实现
 *
 * @author lixunda
 * @version time:2020/3/3 16:26
 */
@Service
public class OrderApplyServiceImpl extends BaseServiceImpl<OrderApplyMapper, OrderApplyDO>
        implements IOrderApplyService {

    private final GoodOrderMapper goodOrderMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public OrderApplyServiceImpl(GoodOrderMapper goodOrderMapper) {
        this.goodOrderMapper = goodOrderMapper;
    }

    /**
     * 店铺调用，根据订单id查询未处理的订单申请数据
     *
     * @param id 订单编号
     * @return 返回申请数据和订单基本数据
     */
    @Override
    public OrderApplyWithOrderVO queryOrderApplyByOrderId(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("订单编号不能为空");
        }
        CacheStore cacheStore = currentStoreCacheNotNull();
        OrderApplyDO orderApplyDO = this.getOne(
                new LambdaQueryWrapper<OrderApplyDO>()
                        .eq(OrderApplyDO::getOrderId, id)
                        .eq(OrderApplyDO::getDealFlag, OrderApplyDO.DEAL_FLAG_NOT_DEAL)
                        .eq(OrderApplyDO::getCancelFlag, OrderApplyDO.CANCEL_FLAG_FALSE)
                , false);
        // 查询订单
        GoodOrderDO goodOrderDO = goodOrderMapper.selectOne(
                new LambdaQueryWrapper<GoodOrderDO>()
                        .eq(GoodOrderDO::getStoreId, cacheStore.getId())
                        .eq(GoodOrderDO::getId, id));
        if (ObjectValidators.isEmpty(goodOrderDO) || ObjectValidators.isEmpty(orderApplyDO)) {
            return null;
        }
        OrderApplyWithOrderVO vo = new OrderApplyWithOrderVO();
        BeanUtils.copyProperties(orderApplyDO, vo);
        vo.setGoodOrderDO(goodOrderDO);
        return vo;
    }

}
